package com.yl.utils;


import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;

public class ReadTextUtil {

    /**
     * 文本转音频
     *
     * @param path   音频生成路径（全路径，带文件名）（例：D:\\aa\\a.mp3）
     * @param text   文本内容
     * @param volume 音量大小 0 - 100
     * @param speed  语音朗读速度 -10 到 +10
     * @return 是否成功
     */
    public static boolean text(String path, String text, int volume, int speed) {
        try {
            // 调用dll朗读方法
            ActiveXComponent ax = new ActiveXComponent("Sapi.SpVoice");
            // 音量 0 - 100
            ax.setProperty("Volume", new Variant(volume));
            // 语音朗读速度 -10 到 +10
            ax.setProperty("Rate", new Variant(speed));
            // 输入的语言内容
            Dispatch dispatch = ax.getObject();
            // 本地执行朗读
            Dispatch.call(dispatch, "Speak", new Variant(text));

            //开始生成语音文件，构建文件流
            ax = new ActiveXComponent("Sapi.SpFileStream");
            Dispatch sfFileStream = ax.getObject();
            //设置文件生成格式
            ax = new ActiveXComponent("Sapi.SpAudioFormat");
            Dispatch fileFormat = ax.getObject();

            // 设置音频流格式
            Dispatch.put(fileFormat, "Type", new Variant(22));
            // 设置文件输出流格式
            Dispatch.putRef(sfFileStream, "Format", fileFormat);
            // 调用输出文件流打开方法，创建一个音频文件
            Dispatch.call(sfFileStream, "Open", new Variant(path), new Variant(3), new Variant(true));
            // 设置声音对应输出流为输出文件对象
            Dispatch.putRef(dispatch, "AudioOutputStream", sfFileStream);
            // 设置音量
            Dispatch.put(dispatch, "Volume", new Variant(volume));
            // 设置速度
            Dispatch.put(dispatch, "Rate", new Variant(speed));
            // 执行朗读
            Dispatch.call(dispatch, "Speak", new Variant(text));
            // 关闭输出文件
            Dispatch.call(sfFileStream, "Close");
            Dispatch.putRef(dispatch, "AudioOutputStream", null);

            // 关闭资源
            sfFileStream.safeRelease();
            fileFormat.safeRelease();
            // 关闭朗读的操作
            dispatch.safeRelease();
            ax.safeRelease();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }



    public static boolean speakingText(String readText) {
        boolean isFinish = true;
        ActiveXComponent sap = new ActiveXComponent("Sapi.SpVoice");
        try {
            sap.setProperty("Volume", new Variant(100));              // 音量 0-100
            sap.setProperty("Rate", new Variant(+2));                 // 语音朗读速度 -10 到 +10
            Dispatch sapo = sap.getObject();                         // 获取执行对象
            Dispatch.call(sapo, "Speak", new Variant(readText));        // 执行朗读
            sapo.safeRelease();                                     // 关闭执行对象
        } catch (Exception e) {
            isFinish = false;
            e.printStackTrace();
        } finally {
            sap.safeRelease();                                      // 关闭执行对象
        }
        return isFinish;
    }

    /**
     * 生成语音文件
     */
    public static void audioFile(String text) {
        try {

            //jacob.dll没成功安装，执行这一步会出错
            //构建音频格式 调用注册表应用
            Dispatch spAudioFormat = new ActiveXComponent("Sapi.SpAudioFormat").getObject();
            //音频文件输出流
            Dispatch spFileStream = new ActiveXComponent("Sapi.SpFileStream").getObject();
            //构建音频对象
            Dispatch spVoice = new ActiveXComponent("Sapi.SpVoice").getObject();

            //设置spAudioFormat音频流格式类型22
            Dispatch.put(spAudioFormat, "Type", new Variant(22));
            //设置spFileStream文件输出流的音频格式
            Dispatch.putRef(spFileStream, "Format", spAudioFormat);

            //设置spFileStream文件输出流参数地址等
            Dispatch.call(spFileStream, "Open", new Variant("D:\\1\\48641486.wav"), new Variant(3), new Variant(true));
            //设置spVoice声音对象的音频输出流为输出文件对象
            Dispatch.putRef(spVoice, "AudioOutputStream", spFileStream);
            //设置spVoice声音对象的音量大小100
            Dispatch.put(spVoice, "Volume", new Variant(100));
            //设置spVoice声音对象的速度 0为正常速度，范围【..-2 -1 0 1 2..】
            Dispatch.put(spVoice, "Rate", new Variant(0));
            //设置spVoice声音对象中的文本内容
            Dispatch.call(spVoice, "Speak", new Variant(text));
            //关闭spFileStream输出文件
            Dispatch.call(spFileStream, "Close");

            //释放资源
            spVoice.safeRelease();
            spAudioFormat.safeRelease();
            spFileStream.safeRelease();

        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

}
